package com.novoda.runbuddy.provider;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.test.AndroidTestCase;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.util.DBUtil;

public class RunProviderTest extends AndroidTestCase {

	private static final String	RUN_BUDDY_DB						= "runBuddyDB1.db";
	private SQLiteDatabase		mdb;

	private final int			RUN1_ID						= 1;
	private final int			RUN1_ROUTEID				= 1;
	private final int			RUN1_CHECKPOINTID			= 1;
	private final String		RUN1_NAME					= "jog01";
	private final int			RUN1_CHCKPOINT_NO			= 1;
	private final int			RUN1_LAP					= 1;
	private final int			RUN1_SEGMENT_TIME			= 6567765;
	private final String		RUN1_CREATED_AT				= "2008-03-10T10:10:00.000";
	private final long			RUN1_TOTALTIME				= 87678687678L;

	private static final Uri	ALL_RUNS_ON_SPECIFIC_DATE	= Uri.parse("content://com.novoda.runbuddy/runs?from=X&to=Y");
	private static final Uri	ALL_RUNS_ON_SPECIFIC_ROUTE	= Uri.parse("content://com.novoda.runbuddy/routes/1/runs");
	private static final Uri	SPECIFIC_RUN_ON_ROUTE		= Uri.parse("content://com.novoda.runbuddy/routes/1/runs/1");
	private static final Uri	ALL_RUNS					= Uri.parse("content://com.novoda.runbuddy/runs");
	private static final String	TAG							= RunProviderTest.class.getName();
	
	
	private DBUtil dbUtil;

	protected void setUp() {
		dbUtil = new DBUtil(this.getContext(), RUN_BUDDY_DB);
		dbUtil.executeDB(R.raw.db_drop);
		dbUtil.createDB();
		dbUtil.insertValuesIntoDB();
		
		if (mdb == null)
			mdb = getContext().openOrCreateDatabase(RUN_BUDDY_DB, Context.MODE_PRIVATE, null);
	}

	public void testRetrievingAllRunsFromContentProvider() {
		
//		String[] cols_latitude = new String[] { RunBuddyCP.Runs._ID, RunBuddyCP.Runs. };
		
		Cursor cur = getContext().getContentResolver().query(RunBuddyCP.Runs.CONTENT_URI, null, null, null, null);
		assertNotNull(cur);

		cur.moveToFirst();
		assertEquals(RUN1_ID, cur.getInt(0));
		assertEquals(RUN1_ROUTEID, cur.getInt(1));
		assertEquals(RUN1_CHECKPOINTID, cur.getInt(2));
		assertEquals(RUN1_NAME, cur.getString(3));
		assertEquals(RUN1_CHCKPOINT_NO, cur.getInt(4));
		assertEquals(RUN1_LAP, cur.getInt(5));
		assertEquals(RUN1_SEGMENT_TIME, cur.getInt(6));
		assertEquals(RUN1_CREATED_AT, cur.getString(7));
		assertEquals(RUN1_TOTALTIME, cur.getLong(8));
		cur.close();
	}

	public void testRetrievingAllRunsBetween_2008_09_01AND_2008_10_06() {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		Cursor cur = null;
		
		String fromParam = "from=2008-09-01T00:00:00.000";
		String toParam = "to=2009-10-06T23:00:00.000";
		
		String query = RunBuddyCP.Runs.CONTENT_URI.toString() + '?' + fromParam + '&' + toParam;
			
		
		cur = getContext().getContentResolver().query(Uri.parse(query), null, null, null, null);
		
		assertNotNull(cur);
		
		cur.moveToFirst();
		assertEquals(RUN1_ID, cur.getInt(0));
		assertEquals(RUN1_ROUTEID, cur.getInt(1));
		assertEquals(RUN1_CHECKPOINTID, cur.getInt(2));
		assertEquals(RUN1_NAME, cur.getString(3));
		assertEquals(RUN1_CHCKPOINT_NO, cur.getInt(4));
		assertEquals(RUN1_LAP, cur.getInt(5));
		assertEquals(RUN1_SEGMENT_TIME, cur.getInt(6));
		assertEquals(RUN1_CREATED_AT, cur.getString(7));
		assertEquals(RUN1_TOTALTIME, cur.getLong(8));

		cur.moveToLast();

		cur.close();
	}
}
